package com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile;
import static com.beerandjesus.sportofkings.handicapper.strategy.RaceAnalysisStrategy.*;

import com.beerandjesus.sportofkings.handicapper.factory.ResultsTableFactory;
import com.beerandjesus.sportofkings.handicapper.model.*;

import java.util.List;

/**
 * Date: Jul 17, 2012
 * Time: 12:08:01 AM
 */
public class ResultsTableFactoryImpl implements ResultsTableFactory {

    /**
     * Get a new ResultsTable for a non-maiden race
     * @return A new empty ResultsTable initialized for a non-maiden race
     */
    public ResultsTable newNonMaidenResultsTable() {
        return new ResultsTableImpl(NON_MAIDEN_TABLE_HEADERS);
    }

    /**
     * Get a new ResultsTable for a maiden race
     * @return A new empty ResultsTable initialized for a maiden race
     */
    public ResultsTable newMaidenResultsTable() {
        return new ResultsTableImpl(MAIDEN_TABLE_HEADERS);
    }

    /**
     * Get a new ResultsTable appropriate to a given RaceType
     * @param raceType The RaceType
     * @return A new empty ResultsTable initailzed for the given RaceType
     */
    public ResultsTable newResultsTable(RaceType raceType) {
        if (raceType == null) {
            throw new IllegalArgumentException("RaceType cannot be null");
        }
        if (raceType.isMaiden()) {
            return newMaidenResultsTable();
        } else {
            return newNonMaidenResultsTable();
        }
    }

    public ResultsTable newResultsTable(RaceType raceType, List<HorseAnalysisResult> results) {
        ResultsTable table = newResultsTable(raceType);
        for (HorseAnalysisResult result : results) {
            result.addToTable(table);
        }
        return table;
    }

    public String getOutputSimpleFormat(List<RaceAnalysisResultVO> races) {
        StringBuffer sb = new StringBuffer("");
        sb.append("\n");
        RaceAnalysisResultVO race1 = races.get(0);
        sb.append(race1.getTrackCode()).append(" ").append(race1.getRaceDate());
        sb.append("\n\n");
        for (RaceAnalysisResultVO race : races) {
            sb.append(race.getTrackCode()).append(" #").
                    append(race.getRaceNumber()).append(": ").
                    append(race.getRaceName()).append(" ").
                    append(race.getPurse()).append("\n");
            sb.append("Surface: ").append(race.getSurface().
                    getDescription()).append("; Par: ").
                    append(race.getSpeedPar()).append("\n");
            sb.append(race.getDistance());
            sb.append("\n\n");
            ResultsTable t = newResultsTable(race.getRaceType(), race.getResults());
            sb.append(t.toStringByType());
            sb.append("\n\n");
        }
        return sb.toString();
    }


}
